package cn.bandeqing.stp;

import cn.bandeqing.base.domain.CurrentAdminUser;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpLogic;

public class StpAdminUtil {

    private StpAdminUtil() {
    }

    public static final String TYPE = "admin";

    public static final String current = "current_admin_user";

    public static StpLogic stpLogic = new StpLogic(TYPE);

    public static void login(CurrentAdminUser currentAdminUser) {
        stpLogic.login(currentAdminUser.getUsId());
        SaSession session = getSession();
        session.set(current, currentAdminUser);
    }

    public static SaSession getSession() {
        return stpLogic.getSession();
    }

    public static CurrentAdminUser getCurrent(boolean flag) {
        if (stpLogic.isLogin()) {
            return (CurrentAdminUser) getSession().get(current);
        }
        // 未登录
        if (flag) {
            throw new NullPointerException("getCurrent == null");
        } else {
            return null;
        }
    }

    public static CurrentAdminUser getCurrent() {
        return getCurrent(false);
    }

    public static void checkLogin() {
        stpLogic.checkLogin();
    }

    public static Long getLoginId() {
        return stpLogic.getLoginIdAsLong();
    }

    public static void logout() {
        stpLogic.logout();
    }

    public static void logout(Long usId) {
        stpLogic.logout(usId);
    }

}
